package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.BusinessProjectParameterItemDto;
import com.cn.bean.dto.ComponentSingleRegistrationDto;
import com.cn.bean.dto.StatusOperationDto;
import com.cn.bean.pojo.lbj.BusinessProjectSingleParameterItemHistoryDataTable;
import com.cn.bean.pojo.lbj.ComponentSingleRegistrationTable;
import com.cn.bean.pojo.sys.BusinessStateOperationSub_Table;
import com.cn.bean.pojo.sys.BusinessStatusOperationMainTable;
import com.cn.bean.pojo.yw.BusinessProjectParameterItemMappingMain;
import com.cn.bean.pojo.yw.BusinessProjectSingleParameterItemCurrentDataTable;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.ComponentSingleRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class ComponentSingleRegistrationController {
    @Autowired
    private ComponentSingleRegistrationService componentSingleRegistrationService;

    @RequestMapping("/select/BusinessProjectParameterItem")
    @ResponseBody
    public List<BusinessProjectParameterItemDto> selectBusinessProjectParameterItem(@RequestBody BusinessProjectParameterItemDto businessProjectParameterItemDto) {
        List<BusinessProjectParameterItemDto> assists = componentSingleRegistrationService.selectBusinessProjectParameterItem(businessProjectParameterItemDto);
        return assists;
    }
    @RequestMapping("/select/ComponentSingleRegistrationTable")
    @ResponseBody
    public List<ComponentSingleRegistrationTable> selectComponentSingleRegistrationTable(@RequestBody ComponentSingleRegistrationTable componentSingleRegistrationTable) {
        List<ComponentSingleRegistrationTable> assists = componentSingleRegistrationService.selectMain(componentSingleRegistrationTable);
        return assists;
    }
    @RequestMapping("/select/ComponentRegistrationTable")
    @ResponseBody
    public List<ComponentSingleRegistrationTable> selectComponentRegistrationTable(@RequestBody ComponentSingleRegistrationTable componentSingleRegistrationTable) {
        List<ComponentSingleRegistrationTable> assists = componentSingleRegistrationService.selectComponentSingleRegistrationTable(componentSingleRegistrationTable);
        return assists;
    }
    @RequestMapping("/select/ComponentSingleRegistrationTableAll")
    @ResponseBody
    public List<BusinessProjectSingleParameterItemCurrentDataTable> selectComponentSingleRegistrationTableAll(@RequestBody BusinessProjectSingleParameterItemCurrentDataTable businessProjectSingleParameterItemCurrentDataTable) {
        List<BusinessProjectSingleParameterItemCurrentDataTable> assists = componentSingleRegistrationService.selectSub(businessProjectSingleParameterItemCurrentDataTable);
        return assists;
    }
    @RequestMapping("/insert/ComponentSingleRegistrationTable")
    @ResponseBody
    public Result insertComponentSingleRegistration(@RequestBody JSONObject jsonObject) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        ComponentSingleRegistrationTable mainTable = new ComponentSingleRegistrationTable();
        Integer id = componentSingleRegistrationService.getComponentSingleRegistrationAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String uniqueCode = jsonObject.getString("uniqueCode");
        ComponentSingleRegistrationTable result=componentSingleRegistrationService.selectUniqueCode(uniqueCode);
        if (result!=null&&uniqueCode.equals(result.getUniqueCode())){
            return Result.failure("501");
        }
        int componentId=jsonObject.getInteger("componentId");
        int singleStatus=jsonObject.getInteger("singleStatus");
        String materialCode=jsonObject.getString("materialCode");
        String supplierUniqueCode=jsonObject.getString("supplierUniqueCode");
        mainTable.setId(id);
        mainTable.setUniqueCode(uniqueCode);
        mainTable.setComponentId(componentId);
        mainTable.setMaterialCode(materialCode);
        mainTable.setSupplierUniqueCode(supplierUniqueCode);
        mainTable.setMaterialStatus(singleStatus);
        int a = componentSingleRegistrationService.insertComponentSingleRegistration(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<ComponentSingleRegistrationDto> djBhList = dataArray.toJavaList(ComponentSingleRegistrationDto.class);
            for (ComponentSingleRegistrationDto dataStructureDto : djBhList) {
                BusinessProjectSingleParameterItemCurrentDataTable subTable = new BusinessProjectSingleParameterItemCurrentDataTable();
                BusinessProjectSingleParameterItemHistoryDataTable historyTable = new BusinessProjectSingleParameterItemHistoryDataTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                BeanUtils.copyProperties(dataStructureDto, historyTable);
                Integer autoid = componentSingleRegistrationService.getCurrentDataTableAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setUniqueCode(uniqueCode);
                subTable.setEntryTimePoint(format.format(date));
                subTable.setDateType(1);
                subTable.setIsdel(0);
                historyTable.setId(autoid);
                historyTable.setUniqueCode(uniqueCode);
                historyTable.setHistoryDataId(autoid);
                historyTable.setEntryTimePoint(format.format(date));
                historyTable.setDateType(1);
                historyTable.setM_2_ID(autoid);
                int b = componentSingleRegistrationService.insertCurrentDataTable(subTable);
                int c = componentSingleRegistrationService.insertHistoryDataTable(historyTable);
                if (b != 1|| c != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/del/ComponentSingleRegistrationTable")
    @ResponseBody
    public Result ComponentSingleRegistrationTable(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("uniqueCode");
        int id=jsonObject.getInteger("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<String> djBhList = djBhArray.toJavaList(String.class);
            for (String uniqueCode : djBhList) {
                    componentSingleRegistrationService.delComponentSingleRegistration(uniqueCode);
                    componentSingleRegistrationService.delCurrentDataTableStatus(uniqueCode);
            }
            return Result.success(200);
        }
        return Result.failure("500", "删除失败");
    }

    @RequestMapping("/update/ComponentSingleRegistrationTable")
    @ResponseBody
    public Result updateComponentSingleRegistrationTable(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        ComponentSingleRegistrationTable mainTable = new ComponentSingleRegistrationTable();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int id = jsonObject.getInteger("id");
        String uniqueCode = jsonObject.getString("uniqueCode");
//        ComponentSingleRegistrationTable result=componentSingleRegistrationService.selectUniqueCode(uniqueCode);
//        if (result!=null&&uniqueCode.equals(result.getUniqueCode())){
//            return Result.failure("500","已经存在相同的单件唯一码");
//        }
        int componentId=jsonObject.getInteger("componentId");
        int singleStatus=jsonObject.getInteger("singleStatus");
        String materialCode=jsonObject.getString("materialCode");
        String supplierUniqueCode=jsonObject.getString("supplierUniqueCode");
        int isdel=jsonObject.getInteger("isdel");
        mainTable.setId(id);
        mainTable.setUniqueCode(uniqueCode);
        mainTable.setComponentId(componentId);
        mainTable.setMaterialCode(materialCode);
        mainTable.setSupplierUniqueCode(supplierUniqueCode);
        mainTable.setMaterialStatus(singleStatus);
        mainTable.setIsdel(isdel);
        int a = componentSingleRegistrationService.updateComponentSingleRegistration(mainTable);
        if (a != 1) {
            return Result.failure("500","更新主表失败");
        }
        List<ComponentSingleRegistrationDto> statusOperationDtos=componentSingleRegistrationService.selectSub(uniqueCode);
        if (statusOperationDtos.size()>0){
            int sub = componentSingleRegistrationService.delCurrentDataTable(uniqueCode);
        }
        if (dataArray==null||dataArray.isEmpty()){
            return Result.success(200);
        }
        List<ComponentSingleRegistrationDto> djBhList = dataArray.toJavaList(ComponentSingleRegistrationDto.class);
        for (ComponentSingleRegistrationDto dataStructureDto : djBhList) {
            BusinessProjectSingleParameterItemCurrentDataTable subTable = new BusinessProjectSingleParameterItemCurrentDataTable();
            BusinessProjectSingleParameterItemHistoryDataTable historyTable = new BusinessProjectSingleParameterItemHistoryDataTable();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            BeanUtils.copyProperties(dataStructureDto, historyTable);
            Integer autoid = componentSingleRegistrationService.getCurrentDataTableAutoid();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setUniqueCode(uniqueCode);
            subTable.setEntryTimePoint(format.format(date));
            subTable.setDateType(2);
            historyTable.setId(autoid);
            historyTable.setUniqueCode(uniqueCode);
            historyTable.setEntryTimePoint(format.format(date));
            historyTable.setDateType(2);
            historyTable.setM_2_ID(autoid);
            int b = componentSingleRegistrationService.insertCurrentDataTable(subTable);
            int c = componentSingleRegistrationService.insertHistoryDataTable(historyTable);
            if (b != 1) {
                return Result.failure("500","更新子表失败");
            }
        }
        return Result.success(200);
    }
}
